/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 * @author TienNT
 */
public class DatabaseAdapter {
        private String mDNSName;
	private String mServerName;
	private int mPort;
	private String mDbName;
	private String mUsername;
	private String mPassword;
	private Connection mConnection;
	private Statement mStatement;
	private ResultSet mResultSet;

	public DatabaseAdapter(String name) throws SQLException {
		mDNSName=name;
                mConnection = DriverManager.getConnection("jdbc:odbc:"+mDNSName); 
	}

	public void driverTest() throws ClassNotFoundException, SQLException {
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                
	}

	private Connection getConnection() throws ClassNotFoundException,
			SQLException {
		if (mConnection == null) {
			driverTest();
			mConnection = DriverManager.getConnection("jdbc:odbc:"+mDNSName); 
		}
		return mConnection;
	}

	private Statement createStatement() throws SQLException,
			ClassNotFoundException {
		if (mStatement == null) {
			mStatement = getConnection().createStatement();
		}
		return mStatement;
	}

	public int executeUpdate(String sqlQuery) throws ClassNotFoundException,
			SQLException {
		return createStatement().executeUpdate(sqlQuery);
	}

	public ResultSet executeQuery(String sqlQuery)
			throws ClassNotFoundException, SQLException {
		mResultSet = createStatement().executeQuery(sqlQuery);
		return mResultSet;
	}

	public void close() throws SQLException {
		if (mResultSet != null) {
			mResultSet.close();
		}
		if (mStatement != null) {
			mStatement.close();
		}
		if (mConnection != null) {
			mConnection.close();
		}
	}
}
